package fr.umlv.ig.tests;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JLabel;

import com.google.gdata.client.maps.MapsService;
import com.google.gdata.data.OtherContent;
import com.google.gdata.data.maps.FeatureEntry;
import com.google.gdata.data.maps.FeatureFeed;
import com.google.gdata.data.maps.MapEntry;
import com.google.gdata.data.maps.MapFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.google.gdata.util.XmlBlob;

public class Test {

	public static String printUserMaps(MapsService myService)
			throws ServiceException, IOException {

		// Request the default metafeed
		final URL feedUrl = new URL("http://maps.google.com/maps/feeds/maps/default/full");
		MapFeed resultFeed = myService.getFeed(feedUrl, MapFeed.class);

		// Print the title of the feed itself.
		System.out.println(resultFeed.getTitle().getPlainText());

		// Iterate through the feed entries (maps) and print out info
		// for each map
		//for (int i = 0; i < resultFeed.getEntries().size(); i++) {
		//MapEntry entry = resultFeed.getEntries().get(i);
		MapEntry entry = resultFeed.getEntries().get(0);
		System.out.println(entry.getTitle().getPlainText());
		System.out.println("  Summary: " + entry.getSummary().getPlainText());
		System.out.println("  Self Link: " + entry.getSelfLink().getHref() +"\n");
		return entry.getSelfLink().getHref();
		//}
	}

	public static void getMapInfo(MapsService myService, String url)
			throws ServiceException, IOException {

		// Request the self link on a particular map
		// Note that you should replace the URL to a self link of
		// a particular map
		final URL mapSelfUrl = new URL(url);
		MapEntry map = myService.getEntry(mapSelfUrl, MapEntry.class);

		System.out.println("\t" + map.getSelfLink().getHref());
		System.out.println(map.getTitle().getPlainText());
		System.out.println(map.getSummary().getPlainText());
	}

	public static void getFeatures(MapsService myService, String url)
			throws ServiceException, IOException {

		// Get a feature feed for a specific map
		final URL featureFeedUrl = new URL(url);
		FeatureFeed featureFeed = myService.getFeed(featureFeedUrl, FeatureFeed.class);

		System.out.println("Features of the Map:");

		for (int i = 0; i < featureFeed.getEntries().size(); i++) {
			FeatureEntry entry = featureFeed.getEntries().get(i);

			// Print the title of the feed itself.
			System.out.println("\nTitle: " + entry.getTitle().getPlainText());
			System.out.println("Self Link: " + entry.getSelfLink().getHref());

			System.out.println("KML:");

			// Print out the KML
			try {
				// Note that the KML is set to type XmlBlob
				XmlBlob kml = entry.getKml();
				System.out.println(kml.getBlob());
			} catch(NullPointerException e) {
				System.out.println("Error: " + e.getClass().getName());
			}
		}
	}

	public static void printFeatureInfo(MapsService myService, String url)
			throws ServiceException, IOException {

		// Get a feature entry from its self URL (returned in the feature feed)
		final URL featureEntryUrl = new URL(url);

		FeatureEntry entry = myService.getEntry(featureEntryUrl, FeatureEntry.class);

		// Print out the KML
		try {
			XmlBlob kml = ((OtherContent) entry.getContent()).getXml();
			System.out.println(kml.getBlob());
		} catch(NullPointerException e) {
			System.out.println("Null Pointer Exception");
		}
	}



	public static void main(String[] args) {

		// Name your service
		MapsService myService = new MapsService("applicationName");
		try {
			// Replace these credentials with your own
			myService.setUserCredentials("bipbip.ir2","bipbip.ir2");
		} catch(AuthenticationException e) {
		}
		try {
			System.out.println("User maps :");
			printUserMaps(myService);
			System.out.println("Bipbip map info :");
			getMapInfo(myService,new String("http://maps.google.com/maps/feeds/maps/201075179825313268815/full/0004bcdfcf186b3791b07"));
			System.out.println("Bipbip map features:");
			getFeatures(myService,new String("http://maps.google.com/maps/feeds/features/201075179825313268815/0004bcdfcf186b3791b07/full"));
			System.out.println("Feature1 info :");
			printFeatureInfo(myService, new String("http://maps.google.com/maps/feeds/features/201075179825313268815/0004bcdfcf186b3791b07/full/0004bceff0425d22096a8"));
		} catch(ServiceException e) {
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		}
		
		
		JFrame frame = new JFrame();
		frame.setSize(650,650);
		
		JLabel label = new JLabel("<html><img src='http://maps.googleapis.com/maps/api/staticmap?size=640x640&maptype=roadmap&sensor=false&markers=size:big|color:red|49.417842,2.826142'></html>");
		
		label.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("Clicked on label at "+e.getX()+";"+e.getY());
				super.mouseClicked(e);
			}
			
		});
		
		frame.getContentPane().add(label,BorderLayout.CENTER);
		
		frame.setVisible(true);

	}

}
